﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;

namespace HanGu.CRM.Areas.CustomerInfo.Controllers
{
    public class MemberContractController : AdminBaseController
    {
        public int ParentID { get; set; }
        public List<HanGu.Entity.MemberContract> sMemberContract
        {
            get
            {
                if (Session[ParentID + "sMemberContract"] != null)
                {
                    return Session[ParentID + "sMemberContract"] as List<HanGu.Entity.MemberContract>;
                }
                else
                {
                    var _sMemberContract = new List<HanGu.Entity.MemberContract>();
                    Session[ParentID + "sMemberContract"] = _sMemberContract;
                    return _sMemberContract;
                }
            }
            set
            {
                foreach (var item in value)
                {
                    sMemberContract.Remove(sMemberContract.Where(c => c.ID == item.ID).FirstOrDefault());
                    sMemberContract.Add(item);
                }
                Session[ParentID + "sMemberContract"] = sMemberContract;
            }
        }

        //
        // GET: /CustomerInfo/MemberContract/
        void InintSelect()
        {
            base.SearchParma.Pagination.IsPaging = false;
            ViewData["Parents"] = new SelectList(GetManagersByLevel(false), "ID", "User"); //servicesManager.GetAll(base.SearchParma);
        }
        public ActionResult Index(int id)
        {
            ParentID = id;
            ViewData["ParentID"] = id;
            InintSelect();
            var memberContracts = serviceMemberInfo.GetContractsByParent(id);
            sMemberContract = memberContracts;
            return View(sMemberContract);
        }


        public ActionResult Create(int? id = null, int? guid = null)
        {
            InintSelect();
            guid = guid ?? 0;
            ParentID = guid.Value;
            var model = new HanGu.Entity.MemberContract() { ParentID = guid.Value };
            if (id.HasValue && id > 0)
            {
                model = serviceMemberInfo.GetContractByID(id.Value);
            }
            else if (id < 0)
            {
                model = sMemberContract.Where(c => c.ID == id).FirstOrDefault();
            }
            
            return View(model);
        }

        //
        // POST: /CustomerInfo/MemberContract/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                HanGu.Entity.MemberContract model = new HanGu.Entity.MemberContract();
                model.CreateTime = DateTime.Now;
                bool result = false;
                TryUpdateModel(model, collection);
                model.Creator = Request["Creator"];
                ParentID = model.ParentID.Value;
                
                if (model.ID > 0)
                {
                    result = serviceMemberInfo.UpdateMemberContract(model);
                }
                else
                {
                    if (model.ID == 0)
                    {
                        if (model.ParentID == 0)
                        {
                            var minModel = sMemberContract.Where(c => c.ID <= 0);
                            model.ID = minModel != null && minModel.Count() > 0 ? minModel.Min(c => c.ID) - 1 : -1;
                            sMemberContract = new List<Entity.MemberContract>() { model };
                        }
                        else
                        {
                            serviceMemberInfo.AddMemberContract(model);
                        }
                    }
                    
                    result = true;
                }
                if (result)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                }
                else
                { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", ""); }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        [HttpPost]
        public string Delete(int id, int? guid = null)
        {
            try
            {
                ParentID = guid != null ? guid.Value : 0;
                if (id < 0)
                {
                    if (sMemberContract.Remove(sMemberContract.Where(c => c.ID == id).FirstOrDefault()))
                    {
                        Session[ParentID + "sMemberContract"] = sMemberContract;
                        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", ""); }

                }
                else
                {
                    if (serviceMemberInfo.DeleteMemberContract(id))
                    {
                        Session[ParentID + "sMemberContract"] = null;
                        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                    }
                    else
                    { ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", ""); }
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";

        }
    }
}
